
package me.zhengjie.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.domain.AppHoliday;
import me.zhengjie.service.AppHolidayService;
import me.zhengjie.service.dto.AppHolidayQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author IQW
* @date 2022-07-18
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "请假管理管理-后台")
@RequestMapping("/api/appHoliday")
public class AppHolidayController {

    private final AppHolidayService appHolidayService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('appHoliday:list')")
    public void exportAppHoliday(HttpServletResponse response, AppHolidayQueryCriteria criteria) throws IOException {
        appHolidayService.download(appHolidayService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询请假管理")
    @ApiOperation("查询请假管理")
    @PreAuthorize("@el.check('appHoliday:list')")
    public ResponseEntity<Object> queryAppHoliday(@RequestParam(defaultValue = "0") int type,
                                                  AppHolidayQueryCriteria criteria, Pageable pageable){

        if(null != criteria.getCreateTime() && criteria.getCreateTime().size() == 0){
            criteria.setCreateTime(null);
        }
        criteria.setUserId1(SecurityUtils.getCurrentUserId().intValue());
        //状态 0待审批、1通过、2不通过、3撤销
        if(type == 3){
            //查看历史
            criteria.setStatusIn(new HashSet(Arrays.asList(1,2)));
        }else{
            //查看待审批
            criteria.setStatus(0);
        }
        return new ResponseEntity<>(appHolidayService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增请假管理")
    @ApiOperation("新增请假管理")
    @PreAuthorize("@el.check('appHoliday:add')")
    public ResponseEntity<Object> createAppHoliday(@Validated @RequestBody AppHoliday resources){
        return new ResponseEntity<>(appHolidayService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改请假管理")
    @ApiOperation("修改请假管理")
    @PreAuthorize("@el.check('appHoliday:edit')")
    public ResponseEntity<Object> updateAppHoliday(@Validated @RequestBody AppHoliday resources){
        appHolidayService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除请假管理")
    @ApiOperation("删除请假管理")
    @PreAuthorize("@el.check('appHoliday:del')")
    public ResponseEntity<Object> deleteAppHoliday(@RequestBody Integer[] ids) {
        appHolidayService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}